package top.superflow.db.orientdb

import scala.concurrent.Await
import scala.concurrent.Future
import top.superflow.db.api.DBServer
import top.superflow.db.api.DBStatus
import top.superflow.exception.ResponseUnsupportedException
import top.superflow.test.AsynTest

import scala.util.{Failure, Success}

class OrientDBBT extends AsynTest{

  val ordb =  OrientDBServer.getInstance
  var startResult: Future[Future[DBServer]] = _
  
  Feature("DB Server Start and Shut down") {
    Scenario("Start and shut down the Orient DB Server") {
      
      Given("Orient DB configuration is")
      val cfg : OrientServerConfiguration = new OrientServerConfiguration("/orientdb/orientdb-server-config.xml")
      info(cfg.getConfigAsString)
      
      When("Starting the Orient DB")
      this.startResult = ordb.startup(cfg)
      
      Then("The Orient DB should be able to be started")
      startResult.onComplete {
        case Success(futureDBServer) =>
          futureDBServer.onComplete {
            case Success(dbServer) => assert(dbServer.getStatus == DBStatus.RUNNING || dbServer.getStatus == DBStatus.STARTING)
            case Failure(ex1) => throw ex1
          }
        case Failure(ex) => throw ex
      }
      
      Then("Within " + stabilizationEpoch * 2 + ", the Orient DB should be in running status.")
      val timeToWaitDBActive = 1 seconds
      val startFuture = Await.result(startResult, timeToWaitDBActive)
      val tmpDBServer  = Await.result(startFuture, timeToWaitDBActive)
      eventually(stabilizationTimeout, stabilizationInterval) {
        assert(tmpDBServer.getStatus == DBStatus.RUNNING, "Orient DB has not been started after secondes set")
      }
      
      Then("The Orient DB should be able to be shut down")
      val shutdownResult : Future[DBServer] = tmpDBServer.shutdown
      shutdownResult map { 
        dbServer => 
          assert(dbServer.getStatus == DBStatus.SHUTDOWN) 
      }
    }
  }
}